package com.sword.demo.messages.interfaces.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

/**
 *
 * 国际化信息接口
 * @author sword
 * @date 2021/12/13 15:11
 */
@RestController
@Data
@RequiredArgsConstructor
@Api(tags = "国际化信息接口")
@RequestMapping("/messages")
@Slf4j
public class MessagesApi {
    /**
     * 国际化信息来源
     */
    private final MessageSource messageSource;

    /**
     * 获取默认语言地区的国际化信息（中文）
     * @return java.lang.String 默认语言地区的国际化信息（中文）
     * @author sword
     * @date 2021/12/13 15:14
     */
    @GetMapping("/getDefault")
    @ApiOperation("获取默认语言地区的国际化信息（中文）")
    public String getDefault() {
        return messageSource.getMessage("welcome", new Object[]{}, Locale.getDefault());
    }

    /**
     * 获取默认语言地区的格式化国际化信息（中文）
     * @return java.lang.String 默认语言地区的格式化国际化信息（中文）
     * @author sword
     * @date 2021/12/13 15:14
     */
    @GetMapping("/getFormatDefault")
    @ApiOperation("获取默认语言地区的格式化国际化信息（中文）")
    public String getFormatDefault() {
        Object[] numberArgs = new Object[]{new Double("0.99")};
        Object[] dateArgs = new Object[]{new Date()};
        Object[] stringArgs = new Object[]{"0.99"};
        Object[] otherArgs = new Object[]{new HashMap<String, String>(3){
            private static final long serialVersionUID = 1L;
            {
                put("key1", "value1");
                put("key2", "value2");
            }
        }};

        Object[] numberArg1 = new Object[]{new Double("-1")};
        Object[] numberArg2 = new Object[]{new Double("-0.5")};
        Object[] numberArg3 = new Object[]{new Double("0")};
        Object[] numberArg4 = new Object[]{new Double("1.1")};
        Object[] numberArg5 = new Object[]{new Double("2")};
        Object[] numberArg6 = new Object[]{new Double("3")};

        return "({0}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format", numberArgs, Locale.getDefault()) + ";  "
                + "({0}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format", dateArgs, Locale.getDefault()) + ";  "
                + "({0}, [String(" + stringArgs[0].toString() + ")]) = " + geFormat("format", stringArgs, Locale.getDefault()) + ";  "
                + "({0}, [Map(" + otherArgs[0].toString() + ")]) = " + geFormat("format", otherArgs, Locale.getDefault()) + ";  "
                + "({0, number}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number", numberArgs, Locale.getDefault()) + ";  "
                + "({0, integer}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.integer", numberArgs, Locale.getDefault()) + ";  "
                + "({0, currency}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.currency", numberArgs, Locale.getDefault()) + ";  "
                + "({0, percent}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.percent", numberArgs, Locale.getDefault()) + ";  "
                + "({0, number, 0.0000}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.SubformatPattern", numberArgs, Locale.getDefault()) + ";  "
                + "({0, date}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date", dateArgs, Locale.getDefault()) + ";  "
                + "({0, date, short}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.short", dateArgs, Locale.getDefault()) + ";  "
                + "({0, date, medium}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.medium", dateArgs, Locale.getDefault()) + ";  "
                + "({0, date, long}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.long", dateArgs, Locale.getDefault()) + ";  "
                + "({0, date, full}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.full", dateArgs, Locale.getDefault()) + ";  "
                + "({0, date, yyyy-MM-dd}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.SubformatPattern", dateArgs, Locale.getDefault()) + ";  "
                + "({0, time}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time", dateArgs, Locale.getDefault()) + ";  "
                + "({0, time, short}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.short", dateArgs, Locale.getDefault()) + ";  "
                + "({0, time, medium}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.medium", dateArgs, Locale.getDefault()) + ";  "
                + "({0, time, long}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.long", dateArgs, Locale.getDefault()) + ";  "
                + "({0, time, full}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.full", dateArgs, Locale.getDefault()) + ";  "
                + "({0, time, HH:mm:ss}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.SubformatPattern", dateArgs, Locale.getDefault()) + ";  "
                + "({0, choice, -1.0#等于负一|0#等于零|1.0<大于一|2#等于二|2<大于二}, [Number(" + numberArg1[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg1, Locale.getDefault()) + ";  "
                + "({0, choice, -1.0#等于负一|0#等于零|1.0<大于一|2#等于二|2<大于二}, [Number(" + numberArg2[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg2, Locale.getDefault()) + ";  "
                + "({0, choice, -1.0#等于负一|0#等于零|1.0<大于一|2#等于二|2<大于二}, [Number(" + numberArg3[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg3, Locale.getDefault()) + ";  "
                + "({0, choice, -1.0#等于负一|0#等于零|1.0<大于一|2#等于二|2<大于二}, [Number(" + numberArg4[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg4, Locale.getDefault()) + ";  "
                + "({0, choice, -1.0#等于负一|0#等于零|1.0<大于一|2#等于二|2<大于二}, [Number(" + numberArg5[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg5, Locale.getDefault()) + ";  "
                + "({0, choice, -1.0#等于负一|0#等于零|1.0<大于一|2#等于二|2<大于二}, [Number(" + numberArg6[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg6, Locale.getDefault()) + ";  "
                ;
    }

    /**
     * 获取美国英文的国际化信息
     * @return java.lang.String 美国英文的国际化信息
     * @author sword
     * @date 2021/12/13 15:14
     */
    @GetMapping("/getEnUs")
    @ApiOperation("获取美国英文的国际化信息")
    public String getEnUs() {
        return messageSource.getMessage("welcome", new Object[]{}, Locale.US);
    }

    /**
     * 获取美国英文的格式化国际化信息
     * @return java.lang.String 美国英文的格式化国际化信息
     * @author sword
     * @date 2021/12/13 15:14
     */
    @GetMapping("/getFormatEnUs")
    @ApiOperation("获取美国英文的格式化国际化信息")
    public String getFormatEnUs() {
        Object[] numberArgs = new Object[]{new Double("0.99")};
        Object[] dateArgs = new Object[]{new Date()};
        Object[] stringArgs = new Object[]{"0.99"};
        Object[] otherArgs = new Object[]{new HashMap<String, String>(3){
            private static final long serialVersionUID = 1L;
            {
                put("key1", "value1");
                put("key2", "value2");
            }
        }};

        Object[] numberArg1 = new Object[]{new Double("-1")};
        Object[] numberArg2 = new Object[]{new Double("-0.5")};
        Object[] numberArg3 = new Object[]{new Double("0")};
        Object[] numberArg4 = new Object[]{new Double("1.1")};
        Object[] numberArg5 = new Object[]{new Double("2")};
        Object[] numberArg6 = new Object[]{new Double("3")};

        return "({0}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format", numberArgs, Locale.US) + ";  "
                + "({0}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format", dateArgs, Locale.US) + ";  "
                + "({0}, [String(" + stringArgs[0].toString() + ")]) = " + geFormat("format", stringArgs, Locale.US) + ";  "
                + "({0}, [Map(" + otherArgs[0].toString() + ")]) = " + geFormat("format", otherArgs, Locale.US) + ";  "
                + "({0, number}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number", numberArgs, Locale.US) + ";  "
                + "({0, integer}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.integer", numberArgs, Locale.US) + ";  "
                + "({0, currency}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.currency", numberArgs, Locale.US) + ";  "
                + "({0, percent}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.percent", numberArgs, Locale.US) + ";  "
                + "({0, number, 0.0000}, [Double(" + numberArgs[0].toString() + ")]) = " + geFormat("format.number.SubformatPattern", numberArgs, Locale.US) + ";  "
                + "({0, date}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date", dateArgs, Locale.US) + ";  "
                + "({0, date, short}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.short", dateArgs, Locale.US) + ";  "
                + "({0, date, medium}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.medium", dateArgs, Locale.US) + ";  "
                + "({0, date, long}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.long", dateArgs, Locale.US) + ";  "
                + "({0, date, full}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.full", dateArgs, Locale.US) + ";  "
                + "({0, date, yyyy-MM-dd}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.date.SubformatPattern", dateArgs, Locale.US) + ";  "
                + "({0, time}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time", dateArgs, Locale.US) + ";  "
                + "({0, time, short}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.short", dateArgs, Locale.US) + ";  "
                + "({0, time, medium}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.medium", dateArgs, Locale.US) + ";  "
                + "({0, time, long}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.long", dateArgs, Locale.US) + ";  "
                + "({0, time, full}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.full", dateArgs, Locale.US) + ";  "
                + "({0, time, HH:mm:ss}, [Date(" + dateArgs[0].toString() + ")]) = " + geFormat("format.time.SubformatPattern", dateArgs, Locale.US) + ";  "
                + "({0, choice, -1.0#equal to minus one|0#equal to zero|1.0<greater than one|2#equal to two|2<greater than two}, [Number(" + numberArg1[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg1, Locale.US) + ";  "
                + "({0, choice, -1.0#equal to minus one|0#equal to zero|1.0<greater than one|2#equal to two|2<greater than two}, [Number(" + numberArg2[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg2, Locale.US) + ";  "
                + "({0, choice, -1.0#equal to minus one|0#equal to zero|1.0<greater than one|2#equal to two|2<greater than two}, [Number(" + numberArg3[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg3, Locale.US) + ";  "
                + "({0, choice, -1.0#equal to minus one|0#equal to zero|1.0<greater than one|2#equal to two|2<greater than two}, [Number(" + numberArg4[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg4, Locale.US) + ";  "
                + "({0, choice, -1.0#equal to minus one|0#equal to zero|1.0<greater than one|2#equal to two|2<greater than two}, [Number(" + numberArg5[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg5, Locale.US) + ";  "
                + "({0, choice, -1.0#equal to minus one|0#equal to zero|1.0<greater than one|2#equal to two|2<greater than two}, [Number(" + numberArg6[0].toString() + ")]) = " + geFormat("format.choice.SubformatPattern", numberArg6, Locale.US) + ";  "
                ;
    }

    /**
     * 获取格式化国际化信息
     * @param code 编码
     * @param args 格式化参数
     * @param locale 本地化对象
     * @return java.lang.String
     * @author sword
     * @date 2021/12/17 11:38
     */
    private String geFormat(String code, Object[] args, Locale locale) {
        return messageSource.getMessage(code, args, locale);
    }
}
